<template>
  <div class="login-container">
    <div class="header-info">
      <div class="info-pc">
        <a href="" class="info-back" @click="backtoLogin">
          <label class="icon-coustom-back icon-control">
            <i class="el-icon-arrow-left"></i>
          </label>
          返回登录
        </a>
      </div>
    </div>
    <div class="main-content">
      <div class="title-container">
        <img src="../../assets/img/logo.png" alt="" class="logo-img" />
      </div>
    </div>
    <div class="step">
      <el-form
        ref="loginForm"
        label-width="100px"
        :model="loginForm"
        :rules="loginRules"
        auto-complete="on"
        style="width: 30%"
      >
        <!-- 手机号获取验证码 -->
        <el-form-item prop="phone" label="手机号">
          <el-input
            ref="phone"
            v-model="loginForm.phone"
            placeholder="请输入手机号"
            name="手机号"
            type="text"
          >
            <template slot="append">
              <el-button v-if="showBtn" class="get-code" @click="getCode">
                获取验证码
              </el-button>
              <span v-else>{{ count }} s</span>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="code" label="验证码">
          <el-input
            v-model="loginForm.code"
            placeholder="请输入验证码"
            name="验证码"
            type="text"
          />
        </el-form-item>
        <el-form-item label="新密码" prop="n">
          <el-input
            v-model="loginForm.n"
            show-password
            placeholder="请输入新密码"
            name="新密码"
            autocomplete="new-password"
            readonly
            onfocus="this.removeAttribute('readonly');this.type='password'"
          />
        </el-form-item>
        <el-form-item label="确认密码" prop="c">
          <el-input
            v-model="loginForm.c"
            show-password
            placeholder="请确认密码"
          />
        </el-form-item>
        <el-button
          type="primary"
          style="width: 30%; margin-left: 45%"
          @click="handleConfirmPwd"
        >
          确 认
        </el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getValidateCode, resetPassWord } from "@/api/useManage";
export default {
  name: "Password",
  data() {
    return {
      showBtn: true,
      count: 60,
      loginRules: {
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern: /^1[3456789]\d{9}$/,
            message: "手机号格式不正确",
            trigger: "blur",
          },
        ],
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }],
        n: [
          { required: true, message: "请输入新密码", trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: "密码在 6 到 20 个字符",
            trigger: "blur",
          },
        ],
        c: [
          { required: true, message: "请确认密码", trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: "密码在 6 到 20 个字符",
            trigger: "blur",
          },
        ],
      },
      loginForm: {
        phone: "", //手机号
        code: "", //验证码
        n: "", //新密码
        c: "", //确认密码
      },
    };
  },
  methods: {
    getCode() {
      this.$refs.loginForm.validateField("phone", (msg) => {
        if (msg == "手机号格式不正确" || msg == "请输入手机号") return;
        let param = {
          phone: this.loginForm.phone,
          scene: 1,
        };
        getValidateCode(param).then(() => {
          this.showBtn = false;
          this.$message({
            message: "验证码已发送",
            type: "success",
          });
          let timer = setInterval(() => {
            this.count--;
            if (this.count <= 0) {
              clearInterval(timer);
              this.showBtn = true;
              this.count = 60;
            }
          }, 1000);
        });
      });
    },
    handleConfirmPwd() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          //两次密码输入不一致
          if (this.loginForm.n !== this.loginForm.c) {
            this.$message({
              message: "两次密码输入不一致",
              type: "error",
            });
            return;
          }
          let params = {
            validateCode: this.loginForm.code,
            passWord: this.loginForm.n,
            phone: this.loginForm.phone,
          };
          resetPassWord(params).then(() => {
            this.$message({
              message: "密码重置成功",
              type: "success",
            });
            this.$router.back();
          });
        }
      });
    },
    backtoLogin() {
      this.loginForm = {
        phone: "", //手机号
        code: "", //验证码
        n: "", //新密码
        c: "", //确认密码
      };
      this.$router.back();
    },
  },
};
</script>

<style lang="scss" scoped>
.title-container {
  text-align: center;
  .logo-img {
    margin-top: -30px;
    width: 200px;
  }
}
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  overflow: hidden;
  .header-info {
    padding: 30px 30px 0;
    .info-back {
      font-size: 15px;
      color: #999;
      text-decoration: none;
      label {
        display: inline-block;
        max-width: 100%;
        margin-bottom: 8px;
        font-weight: 700;
      }
    }
  }
  .step {
    min-height: 500px;
    padding: 10px 0;
    display: flex;
    justify-content: center;
  }

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
